package me.firstapp.repository.shop.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import me.firstapp.module.shop.Address;
import me.firstapp.repository.base.impl.EntityRepositoryImpl;
import me.firstapp.repository.shop.AddressRepository;

@Repository
public class AddressRepositoryImpl extends EntityRepositoryImpl<Address> implements AddressRepository {

	@Override
	protected Class<Address> getPersistentClass() {
		return Address.class;
	}

	@Override
	public Address findAddressByMemberId(Long memberId, Long apiClientId) {
		StringBuilder sql = new StringBuilder(" FROM Address bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);
		sql.append(" AND bean.member.id=:memberId");
		parameters.put("memberId", memberId);

		List<Address> results = super.query(Address.class, sql.toString(), parameters);
		return results.isEmpty() ? null : results.get(0);
	}

}
